package com.droidblackbox.preference;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public final class PreferManager {

	private static PreferManager instance = null;
	private static Map<String, String> map = null;
	public static final String KEY_BOOLEAN_POPUP;
	public static final String KEY_BOOLEAN_AUDIO;
	public static final String KEY_BOOLEAN_SAVE;
	public static final String KEY_BOOLEAN_SMS;
	public static final String KEY_BOOLEAN_LOCATION_SMS;
	public static final String KEY_BOOLEAN_LOCATION_Service;
	public static final String KEY_INT_SEQUENCE;
	public static final String KEY_INT_SHAKE_THRESHOLD;
	public static final String KEY_LONG_MEMORY_SIZE;
	public static final String KEY_STRING_PHONE1;
	public static final String KEY_STRING_PHONE2;
	public static final String KEY_STRING_PHONE3;
	public static final String KEY_STRING_PHONE_MESSAGE;
	public static final String KEY_STRING_SERVER_ADDRESS;

	static {
		KEY_BOOLEAN_POPUP = KeyValues.isPopup.toString();
		KEY_BOOLEAN_AUDIO = KeyValues.isAudio.toString();
		KEY_BOOLEAN_SAVE = KeyValues.isSave.toString();
		KEY_BOOLEAN_SMS = KeyValues.isSms.toString();
		KEY_BOOLEAN_LOCATION_SMS = KeyValues.isLocationSms.toString();
		KEY_BOOLEAN_LOCATION_Service = KeyValues.isLocationService.toString();
		KEY_INT_SEQUENCE = KeyValues.sequence.toString();
		KEY_INT_SHAKE_THRESHOLD = KeyValues.shake.toString();
		KEY_LONG_MEMORY_SIZE = KeyValues.memorySize.toString();
		KEY_STRING_PHONE1 = KeyValues.phone1.toString();
		KEY_STRING_PHONE2 = KeyValues.phone2.toString();
		KEY_STRING_PHONE3 = KeyValues.phone3.toString();
		KEY_STRING_PHONE_MESSAGE = KeyValues.phoneMessage.toString();
		KEY_STRING_SERVER_ADDRESS = KeyValues.serverAddress.toString();

		map = new HashMap<String, String>();
	}

	private enum KeyValues {
		shake, sequence, temp_sequence, isPopup, isAudio, isSave, isSms, phone1, phone2, phone3, serverAddress, phoneMessage, isLocationSms, isLocationService, memorySize
	}

	private Activity activity = null;
	private SharedPreferences sharedPreferences = null;

	public synchronized static PreferManager setInstance(Activity activity) {
		if (instance == null) {
			synchronized (PreferManager.class) {
				if (instance == null) {
					instance = new PreferManager(activity);
				}
			}
		}

		return getInstance();
	}

	private synchronized static PreferManager getInstance() {
		if (instance == null)
			return null;
		instance.getPreference();
		return instance;
	}

	private PreferManager(Activity activity) {
		this.activity = activity;
	}

	private void getPreference() {
		sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(activity);

		boolean isPopup = false;
		boolean isAudio = false;
		int sequence = 0;
		int shakeThreshold = 0;
		boolean isSave = false;
		boolean isSms = false;
		String phone1 = null;
		String phone2 = null;
		String phone3 = null;
		String phoneMessage = null;
		boolean isLocationSms = false;
		boolean isLocationService = false;

		String temp = null;

		long memorySize = 0;
		String serverAddress = null;

		String s = sharedPreferences.getString("shake", "1000");
		temp = sharedPreferences.getString("sequence", "60");
		sequence = (Integer.parseInt(temp)) * 1000;

		isPopup = sharedPreferences.getBoolean("isPopup", true);
		isAudio = sharedPreferences.getBoolean("isAudio", false);
		isSave = sharedPreferences.getBoolean("isSave", true);
		shakeThreshold = Integer.parseInt(s);

		phone1 = sharedPreferences.getString("phone1", "");
		phone2 = sharedPreferences.getString("phone2", "");
		phone3 = sharedPreferences.getString("phone3", "");

		isLocationService = sharedPreferences.getBoolean("isLocationService",
				true);

		String memory = sharedPreferences.getString("memorySize", "100");
		memorySize = Integer.parseInt(memory) * 1024 * 1024;

		serverAddress = sharedPreferences.getString("serverAddress", "");

		isSms = sharedPreferences.getBoolean("isSms", false);
		if (isSms) {
			phoneMessage = sharedPreferences.getString("message", "<None>");
			isLocationSms = sharedPreferences
					.getBoolean("isLocationSms", false);
		}

		map.put(KEY_BOOLEAN_POPUP, String.valueOf(isPopup));
		map.put(KEY_BOOLEAN_AUDIO, String.valueOf(isAudio));
		map.put(KEY_BOOLEAN_SAVE, String.valueOf(isSave));
		map.put(KEY_BOOLEAN_SMS, String.valueOf(isSave));
		map.put(KEY_BOOLEAN_LOCATION_SMS, String.valueOf(isLocationSms));
		map.put(KEY_BOOLEAN_LOCATION_Service, String.valueOf(isLocationService));
		map.put(KEY_INT_SEQUENCE, String.valueOf(sequence));
		map.put(KEY_INT_SHAKE_THRESHOLD, String.valueOf(shakeThreshold));
		map.put(KEY_LONG_MEMORY_SIZE, String.valueOf(memorySize));
		map.put(KEY_STRING_PHONE1, phone1);
		map.put(KEY_STRING_PHONE2, phone2);
		map.put(KEY_STRING_PHONE3, phone3);
		map.put(KEY_STRING_PHONE_MESSAGE, phoneMessage);
		map.put(KEY_STRING_SERVER_ADDRESS, serverAddress);

	}

	public static String get(String key) {
		getInstance();
		if (map.containsKey(key))
			return map.get(key);
		else
			return "";
	}
}
